<template>
  <!-- 需求：
        1：基本头像上传与显示
        2：回显功能
        3：删除
        4：预览
        5：上传进度条

      vue动画
         条件：进入、离开动作  v-if/v-show
         实现
            用 transtion标签包住
            加一个name属性，name='xxx'  是后面css的前缀

            .xxx-enter:起点状态设置，希望从什么状态变到正常状态
            .xxx-enter-active:进入过程中一直存在的class,主体承载  transtion/animation
            .xxx-leave-to:离开状态设置，希望从正常状态变到什么状态
            .xxx-leave-active:离开过程中一直存在的class,主体承载  transtion/animation

-->
  <div class="upload-img">
    <el-upload
      action="#"
      class="upload"
      :http-request="httpRequest"
      :show-file-list="false"
    >
      <i v-if="!value" class="icon el-icon-plus" />
      <img v-else v-imgerror class="avatar" :src="value" alt="">
    </el-upload>
    <i v-if="value" class="del el-icon-delete" @click="delEvent" />
    <i v-if="value" class="big el-icon-zoom-in" @click="showImg = true" />
    <transition name="move">
      <el-progress
        v-if="showProgress"
        class="progress"
        type="circle"
        :percentage="percentage"
        :width="200"
      />
    </transition>
    <!-- 弹窗 -->
    <el-dialog title="预览图片" :visible.sync="showImg">
      <img v-imgerror class="img" :src="value" alt="">
    </el-dialog>
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDNXR8bpeSF1JL8EyZopxRfdHgqGUeuEn8',
  SecretKey: 'HJ9mAbQEn7jgjY9swEI6yAamk7MN8hty'
})
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: String
    }
  },
  data() {
    return {
      showImg: false,
      showProgress: false,
      percentage: 0
    }
  },
  methods: {
    httpRequest(res) {
      this.showProgress = true
      this.percentage = 0
      console.log(res.file)
      const file = res.file
      cos.uploadFile(
        {
          Bucket: 'hr99-1305249343' /* 填写自己的 bucket，必须字段 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: file.name /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */,
          Body: file, // 上传文件对象
          SliceSize:
            1024 *
            1024 *
            1 /* 触发分块上传的阈值，超过5MB使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */,
          onProgress: (progressData) => {
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.showProgress = false
              }, 500)
            }
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          if (err) {
            console.log('上传失败', err)
          } else {
            // this.imgUrl = 'http://' + data.Location
            this.$emit('input', 'http://' + data.Location)
            console.log('上传成功', data)
          }
        }
      )
    },
    async delEvent() {
      await this.$confirm('您确定删除该图片吗?', '温馨提示')
      this.$emit('input', '')
    }
  }
}
</script>
<style lang="scss" scoped>
.upload {
  width: 200px;
  height: 200px;
  overflow: hidden;
  border: 1px dashed #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
  .icon {
    font-size: 30px;
  }
  &:hover {
    border-color: #409eff;
  }
  .avatar {
    width: 100%;
    height: 100%;

    object-fit: contain;
  }
}
.upload-img {
  position: relative;
  display: inline-block;
  .del {
    position: absolute;
    left: 170px;
    top: 5px;
    display: none;
    cursor: pointer;
  }
  .big {
    position: absolute;
    left: 150px;
    top: 5px;
    display: none;
    cursor: pointer;
  }
  &:hover {
    .del,
    .big {
      display: block;
    }
  }
  .img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
  .progress {
    position: absolute;
    left: 0;
    top: 0;
    background-color: #fff;
  }
}

.move-enter,
.move-leave-to {
  opacity: 0;
}
.move-enter-active,
.move-leave-active {
  transition: all 0.5s;
}
</style>
